import { prisma } from '@/lib/prisma'
import { NextResponse } from 'next/server'
import { hash } from 'bcrypt'

export async function PUT(request: Request, { params }: { params: { id: string } }) {
  const { username, role, name, password } = await request.json()
  
  // 如果提供了新密码，则加密
  const data: any = { username, role, name }
  if (password) {
    data.password = await hash(password, 10) // 存储加密后的密码
  }

  const user = await prisma.user.update({
    where: { id: Number(params.id) },
    data: data,
  })
  return NextResponse.json(user)
}

export async function DELETE(request: Request, { params }: { params: { id: string } }) {
  try {
    await prisma.user.delete({
      where: { id: Number(params.id) },
    })
    return NextResponse.json({ message: '用户已删除' })
  } catch (error) {
    console.error('删除用户失败:', error)
    return NextResponse.json({ error: '删除用户失败' }, { status: 500 })
  }
} 